import axios from 'axios';
import constants from '../constants/index';
import configs from './common/configs.js';
import { getTokenHeader } from '../../utils/utils';
import {message} from 'antd';
import Qs from 'qs';
const {GET_USER_IN_SUCCESS,GET_RECRUIT_ID_SUCCESS, GET_RECRUIT_CLASS_SUCCESS} = constants;
const baseUrl = configs.baseUrl;
const acmUrl=configs.acmUrl;

export function userRecruit(query=''){
    return async dispatch=>{
        try{
            let headers=getTokenHeader({});
            axios({
                method: 'POST',
                url: `${baseUrl}resume.do`,
                data: JSON.stringify(query),
                headers: {
                    'Content-Type': 'application/json',
                    ...headers
                    },
                dataType:'json',
            }).then((data)=>{
                if(data.data.code==="SUCCESS"){
                    
                    message.success("基本信息提交成功");
                    dispatch({
                        type: GET_RECRUIT_ID_SUCCESS,
                        data: data.data.data.id,
                    });
                }else{
                    message.error(data.data.msg);
                }
            })
    
        }catch(error){
            message.warning("提交失败");
        }
    }
}

export function getUserIn(query = '',id) {
    let _id=window.localStorage.getItem("userId");
    query={id:_id,page:1,rows:1}
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${acmUrl}?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_USER_IN_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('用户信息获取失败, 请稍后再试');
        }
    };
}

/**
 * 
 * @param {*} query 
 */
export function getRecruit(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}enrollment.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
           // console.log(data.data.pagingList[0].classes);
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type:  GET_RECRUIT_CLASS_SUCCESS,
                    data: data.data.pagingList[0].classes
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('招新信息获取失败, 请稍后再试'); 
        }
    };
}
